<?
include "Stats.php";
include "Contacts.php";
include "Fournisseur.php";

class ContactsController extends Controller {
    
	public $js = array("contacts",
		"jQuery/plugins/jqPlot/jquery.jqplot.min",
		"jQuery/plugins/jqPlot/jqplot.canvasAxisLabelRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasTextRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasAxisLabelRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.dateAxisRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasAxisTickRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.categoryAxisRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.barRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.highlighter.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.cursor.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.dateAxisRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.pieRenderer.min"
	);
	
	public $css = array("contacts","jQuery/jqPlot/jquery.jqplot.min");
	
	public function mainAction(){
		
		$connect = $this->connections;
		$user_id = $_SESSION["user_id"];
		$aClients = $connect->fetchAllModels("Contacts",array("user_id"=>$_SESSION["user_id"]),array("nom"=>"ASC","societe"=>"ASC"));
		$aFournisseurs = $connect->fetchAllModels("Fournisseur",array("user_id"=>$_SESSION["user_id"]),array("nom"=>"ASC","societe"=>"ASC"));
		
		$mysql = $connect->getConnection();
		$query = "SELECT contact_id, count(*) as nb FROM devis WHERE user_id = $user_id GROUP BY contact_id;";
		$res = $mysql->query($query);
		if($mysql->errno != 0) {
			throw new Exception ("Erreur MySQL : ".$mysql->error);
		}
		while($line = $res->fetch_object()){
			$tabCommandeClient[$line->contact_id] = $line->nb;
		}
		$query = "SELECT _fournisseur, count(*) as nb FROM facture_fournisseur WHERE user_id = $user_id GROUP BY _fournisseur;";
		$res = $mysql->query($query);
		if($mysql->errno != 0) {
			throw new Exception ("Erreur MySQL : ".$mysql->error);
		}
		while($line = $res->fetch_object()){
			$tabFactureFournisseur[$line->_fournisseur] = $line->nb;
		}
		$stats = new Stats($mysql);
		$bestCustomer = $stats->bestCustomer();
		$bestFournisseur = $stats->bestFournisseur();
		
		$vars = array(
			"clients"=>$aClients,
			"fournisseurs"=>$aFournisseurs,
			"bestCustomer"=>$bestCustomer,
			"bestFournisseur"=>$bestFournisseur,
			"tabCommandeClient"=>$tabCommandeClient,
			"tabFactureFournisseur"=>$tabFactureFournisseur
			
		);
		return $this->render("contacts/main",$vars);
	}
	public function addClientAction(){
		$connect = $this->connections;
		
		if(isset($_POST["contact_id"])){
			$connect->remove("Contacts",array("_id"=>$_POST["contact_id"]));
			$_POST["_id"] = $_POST["contact_id"];
		}
		
		$contact = new Contacts();
		$contact->fromArray($_POST);
		$contact->user_id = $_SESSION["user_id"];
		$connect->save($contact);
		
		return json_encode(array("response_code"=>"ok"));
	}
	public function addFournisseurAction(){
		$connect = $this->connections;
		
		if(isset($_POST["fournisseur_id"])){
			$connect->remove("Fournisseur",array("_id"=>$_POST["fournisseur_id"]));
			$_POST["_id"] = $_POST["fournisseur_id"];
		}
		
		$contact = new Fournisseur();
		$contact->fromArray($_POST);
		$contact->user_id = $_SESSION["user_id"];
		$connect->save($contact);
		
		return json_encode(array("response_code"=>"ok"));
	}
	public function removeClientAction($client_id){
		$connect = $this->connections;
		$connect->remove("Contacts",array("_id"=>$client_id));
		return json_encode(array("response_code"=>"ok"));
	}
	public function removeFournisseurAction($fournisseur_id){
		$connect = $this->connections;
		$connect->remove("Fournisseur",array("_id"=>$fournisseur_id));
		return json_encode(array("response_code"=>"ok"));
	}
	public function getClientAction($client_id){
		$connect = $this->connections;
		$client = $connect->fetchModels("Contacts",array("_id"=>$client_id));
		$client->response_code = "ok";
		return json_encode($client);
	}
	public function getFournisseurAction($fournisseur_id){
		$connect = $this->connections;
		$fournisseur = $connect->fetchModels("Fournisseur",array("_id"=>$fournisseur_id));
		$fournisseur->response_code = "ok";
		return json_encode($fournisseur);
	}
}
?>